-module(decrypt).
-export([decrypt/2]).

%Decrypt a message that was encrypted using the Vinegere cipjer
decrypt(Message, Key) ->
    DMessage = decrypt_helper(Message,Key, Key, []),
    utils:to_string(DMessage).

decrypt_helper([], _K, _Key, DecriptedSoFar) ->
    lists:reverse(DecriptedSoFar) ;
decrypt_helper(Message, [], Key, DecriptedSoFar) ->
    decrypt_helper(Message, Key, Key, DecriptedSoFar) ;
decrypt_helper([MH|MT], [KH|KT], Key, DecriptedSoFar) ->
    decrypt_helper(MT, KT, Key, [decryptUnit(MH, KH) |DecriptedSoFar]).


decryptUnit(MessageUnit, Key) ->
    Unit = ((MessageUnit - Key) rem 26 ),
    if
	Unit >=0 -> Unit;
	Unit <0  -> Unit + 26
    end.

